/*---------------------------------------------------------------------
 * File name: hal_timer.cpp
 *
 * Copyright (c) <2020-2022>, <ChenLong>
 *
 * All rights reserved.
 *
 * Author: ChenLong
 * Email: worldlong@foxmail.com
 *--------------------------------------------------------------------*/
#include "stm32f30x_misc.h"
#include "stm32f30x_rcc.h"
#include "hal_system.h"
#include "hal_config.h"
/*
*/
void HAL_System::init()
{
  //syscfg power control  
#if EXTI_ENABLE
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
#endif
  
  //DMA power control  
#if DMA1_ENABLE
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
#endif
#if DMA2_ENABLE
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
#endif
  
  //GPIO power control  
#if GPIOA_ENABLE
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
#endif
#if GPIOB_ENABLE
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
#endif
#if GPIOC_ENABLE
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
#endif
#if GPIOD_ENABLE
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
#endif
#if GPIOE_ENABLE
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
#endif
#if GPIOF_ENABLE
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
#endif
#if GPIOG_ENABLE
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOG, ENABLE);
#endif
#if GPIOH_ENABLE
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOH, ENABLE);
#endif

//TIM power control
#if TIM1_ENABLE
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
#endif
#if TIM2_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
#endif
#if TIM3_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
#endif
#if TIM4_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
#endif
#if TIM6_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
#endif
#if TIM7_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
#endif
#if TIM8_ENABLE
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
#endif
#if TIM15_ENABLE
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE);
#endif
#if TIM16_ENABLE
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);
#endif
#if TIM17_ENABLE
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE);
#endif
#if TIM20_ENABLE
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM20, ENABLE);
#endif
  
  //UART power control
#if UART1_ENABLE
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
#endif
#if UART2_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
#endif
#if UART3_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
#endif
#if UART4_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
#endif
#if UART5_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
#endif
  
  //SPI power control
#if SPI1_ENABLE
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
#endif
#if SPI2_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
#endif
#if SPI3_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
#endif
#if SPI4_ENABLE
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI4, ENABLE);
#endif
}
/*
*/
void HAL_System::close_all_periph()
{
  interrupt_disable();
  
  for(int i = 0; i < 8; i++) 
  {
    NVIC->ICER[i] = 0xFFFFFFFF; // Disable interrupt
    NVIC->ICPR[i] = 0xFFFFFFFF; // Clear interrupt flag
  }
  RCC_DeInit();
}
/*
*/
void HAL_System::reset()
{
  close_all_periph();
  NVIC_SystemReset();
}




